<?php
/**
 * User: 苏翔
 * Date: 2024/8/11 19:01
 */

namespace App\Service;

use App\Dao\HscoinBalanceDao;
use App\Dao\HscoinBillDao;
use App\Dao\HscoinStatsDao;
use App\Dao\PriceLogDao;
use App\FundConf;
use App\HscoinConf;

use App\Model\FundBillModel;
use App\Model\HscoinBillModel;
use Hyperf\Di\Annotation\Inject;


/**
 * @method getList($uid = 0, $category = 0, $min_id = 0, $max_id = 0, $size = 10)
 */
class HscoinBillService extends BaseService
{

    #[Inject]
    protected HscoinBillDao $dao;
    #[Inject]
    protected PriceLogDao $priceLogDao;
    #[Inject]
    protected HscoinStatsService $statsService;

    #[Inject]
    protected HscoinBalanceDao $balanceDao;

    function add($category, $link_id, $link_amount, $amount, $uid, $mark = '')
    {
        $this->statsService->init();
        $this->balanceDao->init($uid);

        $bill_id = $this->dao->add($category, $link_id, $link_amount, $amount, $uid, $mark);
        $amount = $this->statsService->getBalance();
        $this->priceLogDao->changeByHscoin($amount, $bill_id);
        return true;
        // 通知价格 !sux
    }

    function cancel($category, $link_id)
    {
        $this->dao->cancel($category, $link_id);
        $amount = $this->statsService->getBalance();
        $this->priceLogDao->changeByHscoin($amount, 0);
    }

    function listBill($page, $limit, $uid, $category, $type)
    {
        dev_log('listBill', compact('page', 'limit', 'uid', 'category', 'type'));
        $model = HscoinBillModel::query();
        if ($uid) {
            $model->where('uid', $uid);
        }
        if ($category) {
            $model->where('category', $category);
        }
        if (1 == $type) {
            $model->where('category', '>', 0);
        } elseif (-1 == $type) {
            $model->where('category', '<', 0);
        }

        $list = $model->orderBy('id', 'desc')->paginate($limit, ['*'], 'page', $page);
        foreach ($list as $k => $v) {
            $list[$k]['category_label'] = $v['category'] > 0
                ? HscoinConf::$categoryIncomeLabelMap[$v['category']] ?? ''
                : HscoinConf::$categoryExpenseLabelMap[$v['category']] ?? '';
        }
        return $list;
    }
}